﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OleDb;
using System.Configuration;
using System.Data;

/// <summary>
/// Summary description for HardwareDBConnection
/// </summary>
public class HardwareDBConnection
{
    OleDbConnection conn;
    OleDbCommand cmd;
    OleDbDataAdapter datatAdapter;
    OleDbDataReader dataReader;
    public String connString = "";

	public HardwareDBConnection()
    {
        conn = new OleDbConnection();
        conn.ConnectionString = ConfigurationManager.ConnectionStrings["AssetDbConnection"].ConnectionString;
        //cmd = new OleDbCommand();
	}



    public Hardware getHardwareByID(int hardwareID)
    {
        connString = "SELECT * FROM Hardware WHERE Hardware_ID = @Hardware_ID";
        cmd = conn.CreateCommand();
        cmd.CommandText = connString;

        conn.Open();

        OleDbParameter computerIDParam = new OleDbParameter();
        computerIDParam.ParameterName = "@Hardware";
        computerIDParam.OleDbType = OleDbType.Numeric;
        computerIDParam.Size = 255;
        computerIDParam.Direction = ParameterDirection.Input;
        computerIDParam.Value = hardwareID;

        cmd.Parameters.Add(computerIDParam);

        //myOleDbCommand.Connection.Open();
        OleDbDataReader dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        Hardware hardware = new Hardware();
        while (dataReader.Read())
        {
            hardware.hardware_ID = Int32.Parse(dataReader["Hardware_ID"].ToString());
            hardware.warranty_ID = Int32.Parse(dataReader["Warranty_ID"].ToString());
            hardware.room_no = Int32.Parse(dataReader["Room_No"].ToString());
            hardware.serial = dataReader["Serial"].ToString();
            hardware.purchase_date = DateTime.Parse(dataReader["Purchase_Date"].ToString());
        }

        closeObjects();

        return hardware;
    }

    public void update(Hardware hardware)
    {
        String connStringUpdate = "UPDATE Hardware SET Room_No = " + hardware.room_no + " WHERE Hardware_ID = " + hardware.hardware_ID + ";";

        cmd = new OleDbCommand(connStringUpdate, conn);
        conn.Open();

        cmd.ExecuteNonQuery();
        closeObjects();
    }
    
    public void updateAll(Hardware hardware)
    {
        String connStringUpdate = "UPDATE Hardware SET Serial = "+hardware.serial+", Purchase_Date = '" + hardware.purchase_date + "', Room_No = " + hardware.room_no + " WHERE Hardware_ID = " + hardware.hardware_ID + ";";

        cmd = new OleDbCommand(connStringUpdate, conn);
        conn.Open();

        cmd.ExecuteNonQuery();
        closeObjects();
    }
    
    public void delete(Hardware hardware)
    {
        OleDbCommand myOleDbCommand;
        OleDbConnection myOleDbConnection = new OleDbConnection();
        myOleDbConnection.ConnectionString = ConfigurationManager.ConnectionStrings["AssetDbConnection"].ConnectionString;
                            
                            //"DELETE Hardware.Hardware_ID FROM Hardware WHERE (((Hardware.Hardware_ID)=10));
        String connString = "DELETE Hardware.Hardware_ID FROM Hardware WHERE Hardware.Hardware_ID = " + hardware.hardware_ID + ";";

        myOleDbCommand = new OleDbCommand(connString, myOleDbConnection);
        myOleDbConnection.Open();

        myOleDbCommand.ExecuteNonQuery();
        closeObjects();
    }
    
    public void closeObjects()
    {
        if (conn.State.ToString().Equals("Open"))
        {
            conn.Close();
        }
        if (cmd.Connection.State.ToString().Equals("Open"))
        {
            cmd.Connection.Close();
        }
    }

}